package per.ethan.javase.java8.optional;

import java.util.Optional;

/**
 * @author Ethan
 * @date 2020/05/06 15:28
 */
public class Demo1 {

    public static void main(String[] args) {
        Girl girl = new Girl();

        // Optional.of(T t), t必须非空,否则会报异常
        Optional<Girl> girlOptional = Optional.of(girl);

        Optional<Object> empty = Optional.empty();
        System.out.println(empty);

        // 可以为null
        Optional<Object> nullOptional = Optional.ofNullable(null);
        System.out.println(nullOptional);

        Girl girl1 = null;
        Optional<Girl> girl2Optional = Optional.ofNullable(girl1);

        Girl girl2 = girl2Optional.orElse(new Girl("杨幂"));
        System.out.println(girl2);
    }

}
